package com.tledu.controller;

import com.tledu.commont.ReqVo;
import com.tledu.commont.ResultVo;
import com.tledu.mapper.SCMapper;
import com.tledu.pojo.Course;
import com.tledu.pojo.SC;
import com.tledu.service.CourseService;
import com.tledu.service.SCService;
import com.tledu.service.SectionService;
import com.tledu.service.impl.SCServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private SectionService sectionService;

    @Autowired
    private SCService scService;

    @Autowired
    private SCMapper scMapper;


    /**
     * 搜索分页，查询课程
     *
     * @param reqVo
     * @return
     */
    @GetMapping("/list")
    public ResultVo getCourseList(ReqVo reqVo) {
        Map<String, Object> map = courseService.find(reqVo);
        //System.out.println(map);
        return new ResultVo(200, "查询成功", map);
    }


    /**
     * 删除课程
     *
     * @param course_id
     * @return
     */
    @DeleteMapping("/deleteCourse/{course_id}")
    public ResultVo deleteCourse(@PathVariable("course_id") Integer course_id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("course_id", course_id);
        //删除课程下的章节
        boolean b = sectionService.removeByMap(map);
        //删除课程
        boolean b1 = courseService.removeByMap(map);
        //删除课程，专题关联表
        boolean b2 = scService.removeByMap(map);

        return new ResultVo(200, "删除成功", null);


    }


    /**
     * 新增课程
     *
     * @param course
     * @return
     */
    @PutMapping("/add")
    public ResultVo save(@RequestBody Course course) {
        boolean save = courseService.save(course);

        List<String> subject_ids = course.getSelectedIds();
        for (String subject_id : subject_ids) {
            SC sc = new SC();
            sc.setSubject_id(Integer.parseInt(subject_id));
            sc.setCourse_id(course.getCourse_id());
            scService.save(sc);
        }


        if (save) {
            System.out.println(course.getCourse_id());

            return new ResultVo(200, "新增成功", null);
        }
        return new ResultVo(500, "新增失败", null);

    }

    /**
     * 修改课程信息
     *
     * @param course
     * @return
     */
    @PutMapping("/updata")
    public ResultVo updata(@RequestBody Course course) {
        // 删除课程表对应的专题
        scService.findSubDelete(course.getCourse_id());
        // 新增课程表对应的专题
        List<String> subject_ids = course.getSelectedIds();
        for (String subject_id : subject_ids) {
            SC sc = new SC();
            sc.setSubject_id(Integer.parseInt(subject_id));
            sc.setCourse_id(course.getCourse_id());
            scService.save(sc);
        }

        boolean b = courseService.updateById(course);
        if (b) {
            return new ResultVo(200, "修改成功", null);
        }
        return new ResultVo(500, "修改失败", null);

    }

    /**
     * 通过课程id获得对应专题信息
     *
     * @param course_id
     * @return
     */
    @GetMapping("/SClist/{course_id}")
    public int[] getSCByCourseId(@PathVariable("course_id") Integer course_id) {
        List<SC> scs = scMapper.find(course_id);
        int[] subjectIds = new int[scs.size()];
        for (int i = 0; i < scs.size(); i++) {
            subjectIds[i] = scs.get(i).getSubject_id();
        }
        return subjectIds;
    }
}
